const robot = require("robotjs")

export namespace control {
    export function hand_keyboard(event: KeyboardEvent) {
        let args: string[] = [];
        let key = get_key(event.key);

        if (event.alt_key) {
            if (key != "alt")
                args.push('alt')
        }
        if (event.shift_key) {
            if (key != "shift")
                args.push('shift')
        }
        if (event.ctrl_key) {
            if (key != "control")
                args.push('control')
        }
        if (event.meta_key) {
            if (key != "command")
                args.push('command')
        }
        if (key) {
            console.log("触发按键", key + args.join(","))
            switch (event.type) {
                case 'keydown': {
                    robot.keyToggle(key, 'down', args)
                }
                case 'keyup': {
                    robot.keyToggle(key, 'up', args)
                }
            }
        }
    }

    export function hand_mouse(event: MouseEvent) {
        switch (event.type) {
            case "mousemove": {
                robot.moveMouse(event.offset_x, event.offset_y)
                break
            }
            case "mousedown":
                {
                    let key = get_mouse_key(event.button)
                    robot.mouseToggle("down", key)
                }
            case "mouseup":
                {
                    let key = get_mouse_key(event.button)
                    robot.mouseToggle("up", key)
                    break
                }
            case "wheel": {
                console.log("滚动鼠标", event.type)
                robot.scrollMouse(event.delta_x, event.offset_y);
                break
            }
            default: {
                console.log("鼠标事件", event.type)
                break
            }
        }
    }

    export function get_mouse_key(button: number): string {
        switch (button) {
            case 0: {
                return 'left';
            }
            case 1: {
                return "middle";
            }
            case 2: {
                return "right";
            }
            default:
                return 'left';
        }
    }

    export function get_key(key: string): string {
        if (key.length == 1) {
            return key
        }
        let result = keys[key];
        if (!result) {
            result = key;
        }
        return result.toLowerCase()
    }

    export const keys: Record<string, string> = {
        "Numpad0": "numpad_0",
        "Numpad1": "numpad_1",
        "Numpad2": "numpad_2",
        "Numpad3": "numpad_3",
        "Numpad4": "numpad_4",
        "Numpad5": "numpad_5",
        "Numpad6": "numpad_6",
        "Numpad7": "numpad_7",
        "Numpad8": "numpad_8",
        "Numpad9": "numpad_9",
        "ArrowDown": "down",
        "ArrowUp": "up",
        "ArrowLeft": "left",
        "ArrowRight": "right",
        "Meta": "command"
    }
}

export interface MouseEvent {
    type: string;
    offset_x: number;
    offset_y: number;
    button: number;
    delta_x: number;
    delta_y: number;
}

export interface KeyboardEvent {
    type: string;
    key: string;
    code: string;
    ctrl_key: boolean;
    alt_key: boolean
    shift_key: boolean;
    meta_key: boolean;
    repeat: boolean;
}